	var chatDoc;
	var chatFrame;
	var nick = 'nick';

	//window.onload = chat_init;

	/*
	 * initialize all the chat need 
	 */
	function chat_init() {
		//alert("into chat");
		enterChat();
		//document.forms[0].msg.focus();
	}
	
	/*
	 * Join a listener and publish a enter information to OTHERS
	 */
	function enterChat() {
		p_join();
		p_listen('/chat');
		alert("into chat");
		//p_publish('/chat', 'action', 'enter', 'nick', nick);
	}

	// Event Callback for join
	function onJoinAck(event) {
		//appendMessage('Listening to chat');
		alert('Listening to chat');
	}

	// Event Callback: display all events
	function onData(event) {
		alert("onData");
		p_debug(false, "pushlet-app", 'event received event='
				+ event.getEvent());
		var action = event.get('action');
		var content = 'none action=' + action;
		if (action == 'send') {
			content = '<b>' + event.get('nick') + '</b>: <i>'
					+ event.get('msg') + '</i>';
		} else if (action == 'enter') {
			content = '<b><i>*** ' + event.get('nick')
					+ ' joined chat ***</i></b>';
		} else if (action == 'exit') {
			content = '<b><i>*** ' + event.get('nick')
					+ ' left chat ***</i></b>';
		}
		alert(content);
		//appendMessage(content);
	}

	// Event Callback: display all events
	function onNack(event) {
		alert('negative response from server: ' + event.getEvent()
				+ ' reason: ' + event.get('p_reason'));
	}

	function appendMessage(content) {
		var newDiv = chatDoc.createElement("DIV");
		newDiv.innerHTML = content;
		chatDoc.getElementById("contents").appendChild(newDiv);
		chatFrame.scrollTo(0, chatDoc.getElementById("contents").offsetHeight);
	}

	function sendMsg() {
		//alert("Ready to send MSG");
		//p_publish('/chat', 'action', 'send', 'nick', nick, 'msg', document
				//.getElementById("insConSending").value, 'toSomebody', 6);
		//resetForm();
	}

	/*
	 * End this chat, publish to others.
	 */
	function leaveChat() {
		p_publish('/chat', 'action', 'exit', 'nick', nick);// Send exit to chatters
		p_leave();// Stop pushlet session
		setTimeout('gotoEnter()', 500);// Give some time to send the leave request to server
	}

	function gotoEnter() {
		window.location.href = 'enter.html';
	}

	function resetForm() {
		document.forms[0].msg.value = "";
		document.forms[0].msg.focus();
	}